<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="searchForm" :inline="true">
        <el-form-item label="打印名称：" prop="name">
          <el-input
            v-noSpace
            v-model="searchForm.name"
            class="!w-[192px]"
            clearable
            @keyup.enter="handleQuery"
            placeholder="请输入打印名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="时间范围：" prop="timeScopes">
          <el-date-picker
            v-model="searchForm.timeScopes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="flex-1"
            clearable
            @change="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-card shadow="never">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="code" label="唯一编号" align="center" />
        <el-table-column prop="name" label="打印名称" align="center" />

        <el-table-column label="信息内容" align="center">
          <template #default="scope">
            {{ scope.row.infoQrCode }}
          </template>
        </el-table-column>
        <el-table-column label="追溯二维码" align="center">
          <template #default="scope">
            <vue-qr
              ref="qrCode"
              :text="scope.row.traceQrCode"
              :logoScale="40"
              :size="40"
              @click="handleViewQRCode(scope.row.traceQRCode)"
            ></vue-qr>
          </template>
        </el-table-column>
        <el-table-column prop="printTime" label="打印时间" align="center" />
        <el-table-column label="打印状态" align="center">
          <template #default="scope">
            <el-tag
              :type="scope.row.printStatus === 1 ? 'success' : 'danger'"
              class="whitespace-nowrap"
            >
              {{ scope.row.status ? "成功" : "失败" }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="searchForm.pageNum"
        v-model:limit="searchForm.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { CircleCheck, CircleClose, Link } from "@element-plus/icons-vue";
import vueQr from "vue-qr/src/packages/vue-qr.vue";
import productLabelApi, {
  productLabelPageQuery,
  productLabelPageVO,
} from "@/api/prodManage/productLabel";
import productBomAPI from "@/api/productBom/productBom";

const total = ref(0);

const searchForm = ref<productLabelPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const tableData = ref<productLabelPageVO[]>();
const optionsProductName = ref<{ value: string; label: string }[]>([]); // 显式指定类型
const getParentCode = () => {
  productBomAPI
    .parentCod({ pageNum: 1, pageSize: 20 })
    .then((data) => {
      optionsProductName.value = (data?.list as any).map((item: any) => ({
        value: item.code,
        label: item.productName,
      }));
    })
    .catch((err: any) => {
      // ElMessage.error(`${err}`);
    });
};

const handleQuery = () => {
  productLabelApi.getPage(searchForm.value).then((res) => {
    tableData.value = res.list;
    total.value = res.total;
  });
};

const handleReset = () => {
  searchForm.value = {
    pageNum: 1,
    pageSize: 10,

    name: "",
    timeScopes: "",
  };
};

const handleViewQRCode = (url: string) => {
  window.open(url, "_blank");
};
onMounted(() => {
  handleQuery();
});
</script>

<style scoped></style>
